// JavaScript Document
$(document).ready(function(e) {
	queryCategorys("");
	/************** element handler ********************************/
	
	function menu_click_handler(e) {
		$(this).next().toggle(500);
	}
	
	function subMenu_mouseover_handler(e) {
		$(this).addClass("over");
	}
	
	function subMenu_mouseout_handler(e) {
		$(this).removeClass("over");
	}
	
	var categoryId;
	var clickSubMenu;
	function subMenu_click_handler(e) {
		if(clickSubMenu) {
			clickSubMenu.removeClass("down");
		}
		clickSubMenu = $(this);
		clickSubMenu.addClass("down");	
		$("div#productDetail").hide();
		$("div#productList").show();
		$("div#page").show();
		categoryId = $(this).attr("data-id");
		var params = [{property:"l.languageId", value:language, condition:"eq"}, 
					  {property:"p.productCategoryId", value:categoryId, condition:"eq"},
					  {property:"p.productStatus", value:1, condition:"eq"}
					 ];
		queryProducts(params, 6, 1);
	}

	/************** element handler end********************************/
	
    /************* category -- start *****************/
	function queryCategorys(params) {
		$.getJSON(ctx+"/category/getAll.do", function(res){
			if(res) {
				$("ul#category").empty();
				$.each(res, function(index, category) {
//					if(category.languageId == language && category.categoryType != "3") {
//						db_menu_handler(category.subCategorys, index);
//					}
					if(category.categoryType != "3" && category.languageId == language) {
						result_handler(category.subCategorys, index); //三級菜單處理
					}
				});
			}
		});
	}
	
	//一級菜單處理
	function single_menu_handler(index, d) {
		var li = $("<li>").addClass("menu")
						.attr("data-id", d.productCategoryId)
						.text(d.categoryName)
						.on("click", subMenu_click_handler)
						.on("mousemove",subMenu_mouseover_handler)
					    .on("mouseout",subMenu_mouseout_handler);
		$("ul#category").append(li);
		if(index == 0) {
			li.click();
		}
	}
	
	//二级菜单处理
	function db_menu_handler(categroys, parentIndex) {
		$.each(categroys, function(index, d) {
			var li = $("<li>").addClass("menu")
			.attr("data-id", d.productCategoryId)
			.text(d.categoryName)
			.on("click", subMenu_click_handler)
			.on("mousemove",subMenu_mouseover_handler)
		    .on("mouseout",subMenu_mouseout_handler);
			$("ul#category").append(li);
			if(parentIndex == 0 && index == 0) {
				li.click();
			}
		}); 		
	}
	
	//三級菜單處理
	function result_handler(data, position) {
		$.each(data, function(index, category) {
			$("ul#category").append($("<li>").addClass("menu").text(category.categoryName).on("click", menu_click_handler));
			var sub_ul = $("<ul>").addClass("sub");
			$.each(category.subCategorys, function(i, subs) {
				var li = $("<li>").addClass("subMenu").attr("data-id", subs.productCategoryId).text(subs.categoryName)
						 		  .on("mousemove",subMenu_mouseover_handler)
								  .on("mouseout",subMenu_mouseout_handler)
		     					  .on("click",subMenu_click_handler);
				li.appendTo(sub_ul);
				if(position==0 && i == 0 && index == 0) {
					li.click();
				}
			});
			$("ul#category").append(sub_ul);
			if(position==0 && index == 0) {
				$("li.menu").click();
			}
		});
	}
	/************* category -- end *****************/
	
	/************ product -- start *************************/
	//根據產品類別ID獲取產品
	var products;
	function queryProducts(params, pageSize, pageNo) {
		$.ajax ({
			url : ctx+"/product/query.do",
			type : "post",
			dataType:"json",	
			data : {
				pageNo : pageNo,
				pageSize : pageSize,
				orderType : "desc",
				orderBy : "isTop",
				rules : $.toJSON(params)
			},
			success : function(response) {
				products = response.result;
				updatePageLabel(response.pageNo,Math.ceil(response.totalCount/pageSize));
				query_products_handler(response.result);
			},
			error : function(response) {
				alert(response.status + " - " + response.statusText);
			}
		});		
	}
	
	function query_products_handler(result) {
		$("div#productList").empty();
		$.each(result, function(index, product) {
			var found = false;
			var div = $("<div>").addClass("product")
						  .append($("<p>").addClass("desc").text(product.productModel))
						  .append($("<p>").addClass("title").text(product.productName))
						  .attr("data-pid", product.productId)
						  .on("click", queryProductByPid);
			if(product.files.length > 0) {
				$.each(product.files, function(i, file){
					if(1 == file.isMainFile) {
						found = true;
						div.append($("<img>").css("padding","12px 1px 1px 2px")
								.attr("width","355px").attr("height","180px")
								.attr("src", ctx + "/home/imagesrc.do?fileInfoId=" + file.uploadFileInfoId)
								.attr("alt", file.fileName));						
					}
				});
				if(!found) {
					div.append($("<img>").css("padding","12px 1px 1px 2px")
							.attr("width","355px").attr("height","180px")
							.attr("src", ctx + "/home/imagesrc.do?fileInfoId=" + product.files[0].uploadFileInfoId)
							.attr("alt", product.files[0].fileName));						
				}
			}						  
			$("div#productList").append(div);
		});
	}
	
	//根據產品ID獲取產品詳細信息
	function queryProductByPid(e) { 
		$("div#productList").hide(); 
		$("div#page").hide();
		$("div#productDetail").show();
		var pid = $(this).attr("data-pid");
		$.ajax({
			url : ctx + "/product/web/productDetail/" + pid + ".do",
			type : "post",
			dataType:"html",
			success : function(response) {
				//$("div#detail p").empty();
				$("div#detail p").html(response);
			},
			error : function(response) {
				alert(resrponse.status + " - " + response.statusText);
			}		
		});
		$.each(products, function(index, product) {
			if(pid == product.productId) {
				var pos = 1;
				$("td#samll_4").empty();
				$("td#samll_1").empty();
				$("td#samll_2").empty();
				$("td#samll_3").empty();
				$("td#modelText").text(product.productNo);
				$("td.remark").text(product.productModel);
				$("td.productName").text(product.productName);
				$.each(product.files, function(j, file) {
					if(1 != file.isMainFile) {
						$("td#big").empty();
						$("td#big").append($("<img>")
								   .attr("src", ctx + "/home/imagesrc.do?fileInfoId=" + file.uploadFileInfoId)
								   .attr("alt", file.fileName));
						$("td#samll_" + pos)
						.append($("<img>").attr("height","62px").attr("width","120px")
						.attr("src", ctx + "/home/imagesrc.do?fileInfoId=" + file.uploadFileInfoId)
						.attr("alt", file.fileName).attr("data-fileInfoId",file.uploadFileInfoId)
						.on("click", switchImage)
						.on("mouseover", function(e){$(this).addClass("td_over");})
						.on("mouseout", function(e){$(this).removeClass("td_over");}));	
						pos = pos + 1;							   						
					}
					/*if(j > 0) {
						if(j == 1) {
							$("td#big").empty();
							$("td#big").append($("<img>")
									   .attr("src", ctx + "/home/imagesrc.do?fileInfoId=" + file.uploadFileInfoId)
									   .attr("alt", file.fileName));
						}
						$("td#samll_" + j)
						.append($("<img>").attr("height","62px").attr("width","120px")
						.attr("src", ctx + "/home/imagesrc.do?fileInfoId=" + file.uploadFileInfoId)
						.attr("alt", file.fileName).attr("data-fileInfoId",file.uploadFileInfoId)
						.on("click", switchImage)
						.on("mouseover", function(e){$(this).addClass("td_over");})
						.on("mouseout", function(e){$(this).removeClass("td_over");}));
					}*/
				});
				return false;
			}
		});
	}
	
	function switchImage(e) {
		$("td#big img").attr("src", ctx + "/home/imagesrc.do?fileInfoId=" + $(this).attr("data-fileInfoId"))
					   .attr("alt", file.fileName);
	}
	
	//分頁處理 --- start
	var begin = 0;
	function updatePageLabel(pageNo, totalPage) {
		$("#tr_page").empty();
		if(totalPage == 0) {		
			return;
		}
		if(totalPage <= 5) {
			createPageLabel(1,(totalPage+1),pageNo);
		} else {
			if((pageNo < 5)) {
				createPageLabel(1,6,pageNo);
			} else { 
				if(((pageNo - 3) % 2) == 0) {
					begin = (pageNo + 2) > totalPage ? (totalPage - 4) : pageNo - 2;
				} 
				createPageLabel(begin,(begin+5),pageNo);
			}				
			if((pageNo + 2) < totalPage) {
				$("<td style='color:#666666'>...</td>").appendTo("#tr_page");
				$("<td>").append($("<a>").attr({href:"javascript:void(0)", title:totalPage}).text(totalPage))
						 .appendTo("#tr_page");
			}
		}
		$("<td style='color:#666666'>&gt;</td>").appendTo("#tr_page");
		pageClick();
	}
	
	function createPageLabel(begin, end, pageNo) {
		$("<td style='color:#666666'>&lt;</td>").appendTo("#tr_page");
		for(var i=begin; i<end; i++) {
			if(i != pageNo) {
				$("<td>").append($("<a>").attr({href:"javascript:void(0)", title:i}).text(i)).appendTo("#tr_page");
			} else {
				$("<td>").append($("<a>").attr({href:"javascript:void(0)", title:i}).text(i).css("color","#F00"))
						 .appendTo("#tr_page");
			}
		}
	}
	
	function pageClick() {
		$("#tr_page a").on("click",function(e) {
			queryProducts([{property:"l.languageId", value:language, condition:"eq"}, 
						   {property:"p.productCategoryId", value:categoryId, condition:"eq"},
						   {property:"p.productStatus", value:1, condition:"eq"}], 
						   6, 
						   parseInt($(this).attr("title")));
		});
	}	
	//分頁處理end	
	
}); 